/*
* Copyright 2007-2011 AKABANA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package net.akb7.utils
{
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.IEventDispatcher;
    
    import mx.core.IFlexModuleFactory;
    import mx.core.mx_internal;
    import mx.events.FlexEvent;
    import mx.managers.ISystemManager;
    import mx.utils.MediaQueryParser;
    
    /**
     * CSS Media Queryで高さや幅も取り扱えるように標準のMediaQueryParserを拡張したクラス
     * 
     * 式：@media (prop:value)     条件：prop=value
     * 式：@media (min-prop:value) 条件：this[ prop ] > value
     * 式：@media (max-prop:value) 条件：this[ prop ] < value
     * 
     */
    public class EnhancedMediaQueryParser extends MediaQueryParser{
        
        private static function clearCache(cache:Object):void{
            for (var key:String in cache) 
            {
                if( key.indexOf("width") >= 0 || key.indexOf("height") >= 0){
                    cache[key] = null;
                    delete cache[key];
                }
            }
        }
        
        /**  
         * ステージの幅
         * 
         * width, min-width, max-width
         */
        public var width:Number;
        
        /**
         * ステージの高さ
         *
         * height, min-height, max-height
         */
        public var heigth:Number;
        
        /** 
         * フルスクリーン幅（デバイス幅）
         *
         * device-width, min-device-width, max-device-width
         */
        public var deviceWidth:Number;
        
        /**
         * フルスクリーン幅（デバイス幅）
         *
         * device-height, min-device-height, max-device-height
         */
        public var deviceHeight:Number;
        
        /**  
         * デバイスのアスペクト比
         * 
         * device-aspect-ratio, min-device-aspect-ratio, max-device-aspect-ratio
         */
        public var deviceAspectRatio:Number;
        
        /**
         * コンストラクタ
         * 
         */
        public function EnhancedMediaQueryParser(moduleFactory:IFlexModuleFactory=null,resizeUpdate:Boolean=false){
            super(moduleFactory);
            
            var stage:Stage = (moduleFactory as ISystemManager).stage;
            if( resizeUpdate ){
                stage.addEventListener(Event.RESIZE,stage_resizeHandler,false,int.MAX_VALUE);
            }
            update(stage);
        }

        private function stage_resizeHandler(event:Event):void{
            clearCache( mx_internal::goodQueries );
            clearCache( mx_internal::badQueries );
            update(event.target as Stage);
        }
        
        /**
         * @private 
         */
        protected function update(stage:Stage):void{
            this.width = stage.stageWidth;
            this.heigth = stage.stageHeight;
            this.deviceWidth = stage.fullScreenWidth;
            this.deviceHeight = stage.fullScreenHeight;
            this.deviceAspectRatio = deviceWidth/deviceHeight;
        }
    }
}